package com.elsa.DataStatistic;

import com.elsa.DataStatistics.DataStatisticsApplication;
import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.*;
import com.elsa.DataStatistics.statistics.*;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : testDay  //类名
 * @Author : elsa //作者
 */
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = DataStatisticsApplication.class)
@Slf4j
public class testDay {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayStatistics dayStatistics;

    @Autowired
    private DayMenuStatistics dayMenuStatistics;

    @Autowired
    private DayMenuMapper dayMenuMapper;

    @Autowired
    private MonthMenuStatistics monthMenuStatistics;

    @Autowired
    private YearMenuStatistics yearMenuStatistics;

    @Autowired
    private DayMpMapper dayMpMapper;

    @Autowired
    private DayMPStatistics dayMPStatistics;

    @Autowired
    private MonthMPStatistics monthMPStatistics;

    @Autowired
    private  YearMPStatistics yearMPStatistics;

    @Autowired
    private DayPersonalMapper dayPersonalMapper;

    @Autowired
    private DayPersonalStatistics dayPersonalStatistics;

    @Autowired
    private MonthPersonalStatistics monthPersonalStatistics;

    @Autowired
    private YearPersonalStatistics yearPersonalStatistics;

    @Test
    public void testSelectAllOrderInOrderById() {
        log.info("订单总支出：" + calculateUtil.getOrderTotalExpendByOId("27"));
        log.info("订单总利润：" + calculateUtil.getOrderTotalProfitByOId("27"));
        log.info("订单总收入：" + calculateUtil.getOrderTotalIncomeByOId("27"));
    }

    @Test
    public  void testDayMenu() {

//        int dId = timeUtil.getDayTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics("27");

        for (int i = 0; i < menuOrderList.size(); i++) {
            DayMenu dayMenu = dayMenuMapper.selectDayMenuMapperFromDIdAndMId(20230327, menuOrderList.get(i).getMId());
            if(dayMenu == null) {
                dayMenu = new DayMenu();
                dayMenu.setDmId(dayMenuMapper.selectCount(null) + 1);
                dayMenu.setDId(20230327);
                dayMenu.setMId(menuOrderList.get(i).getMId());
                dayMenu.setMName(menuOrderList.get(i).getMName());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayMenu.setDmIncome(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                dayMenu.setDmExpand(menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                double profit = dayMenu.getDmIncome() - dayMenu.getDmExpand();
                dayMenu.setDmProfit(profit);
                dayMenu.setDmNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(dayMenu));
                dayMenuMapper.insert(dayMenu);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayMenu.setDmId(dayMenu.getDmId());
                dayMenu.setMName(menuOrderList.get(i).getMName());
                dayMenu.setDmIncome(dayMenu.getDmIncome() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                dayMenu.setDmExpand(dayMenu.getDmExpand() + menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                double profit = (menu.getMPrice() * menuOrderList.get(i).getMoNum()) - (menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                dayMenu.setDmProfit(dayMenu.getDmProfit() + profit);
                dayMenu.setDmNum(dayMenu.getDmNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(dayMenu));
                dayMenuMapper.updateById(dayMenu);
            }
        }

     }

    @Test
    public void testDayMenuStatistics() {
        dayMenuStatistics.dayStatistics("27");
    }

    @Test
    public void testMonthMenuStatistics() {
        monthMenuStatistics.monthStatistics("27");
    }

    @Test
    public void testYearMenuStatistics() {
        yearMenuStatistics.yearStatistics("27");
    }

    @Test
    public void testDayMenuPersonStatistics() {
        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics("27");

        for (int i = 0; i < menuOrderList.size(); i++) {
            DayMp dayMp = dayMpMapper.selectDayMpByDIdAndMIdAndCId(20230325, menuOrderList.get(i).getCId(), menuOrderList.get(i).getMId());
            if(dayMp == null) {
                dayMp = new DayMp();
                dayMp.setDmpId(dayMpMapper.selectCount(null) + 1);
                dayMp.setDpId(20230325);
                dayMp.setMId(menuOrderList.get(i).getMId());
                dayMp.setMName(menuOrderList.get(i).getMName());
                dayMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayMp.setDmpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayMp.setDmpNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(dayMp));
                dayMpMapper.insert(dayMp);
            } else {
                dayMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayMp.setDmpCalorie(dayMp.getDmpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayMp.setDmpNum(dayMp.getDmpNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(dayMp));
                dayMpMapper.updateById(dayMp);
            }
        }
    }

    @Test
    public void testDayMenuPersonStatistics1() {
        dayMPStatistics.dayMPStatistics("27");
    }

    @Test
    public void testMonthMenuPersonStatistics1() {
        monthMPStatistics.monthMPStatistics("27");
    }

    @Test
    public void testYearMenuPersonStatistics1() {
        yearMPStatistics.yearMPStatistics("27");
    }

    @Test
    public void testDayPersonalStatistics() {
        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics("27");

        for (int i = 0; i < menuOrderList.size(); i++) {
            DayPersonal dayPersonal = dayPersonalMapper.selectDayPersonalByDIdAndCId(20230325, menuOrderList.get(i).getCId());
            if(dayPersonal == null) {
                dayPersonal = new DayPersonal();
                dayPersonal.setDpsId(dayPersonalMapper.selectCount(null) + 1);
                dayPersonal.setDpId(20230325);
                dayPersonal.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayPersonal.setDpExpend(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                dayPersonal.setDpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayPersonalMapper.insert(dayPersonal);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayPersonal.setDpExpend(dayPersonal.getDpExpend() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                dayPersonal.setDpCalorie(dayPersonal.getDpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayPersonalMapper.updateById(dayPersonal);
            }
        }
    }

    @Test
    public void testDayPersonalStatistics1() {
        dayPersonalStatistics.dayPersonalStatistics("27");
    }

    @Test
    public void testMonthPersonalStatistics1() {
        monthPersonalStatistics.monthPersonalStatistics("27");
    }

    @Test
    public void testYearPersonalStatistics1() {
        yearPersonalStatistics.yearPersonalStatistics("27");
    }



}
